/*
 ------------------------------------------------------------------------------
 Copyright (C) 2009-2011 Eternal Games.

 This file is part of the Eternal Tech source code.

 The Eternal Tech source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Eternal Tech source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Eternal Tech source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


//
// fileFormats.h - ...
//


#if !defined(__FILEFORMATS_H__) || !INCLUDE_GUARDS
#define __FILEFORMATS_H__


/*
 ==============================================================================

    PAK files are used for compressed zip files

 ==============================================================================
*/

#define PAK_IDENT			(('K'<<24)+('C'<<16)+('A'<<8)+'P')

struct pakFile_t {
	char			name[56];
	int				filePos;
	int				fileLen;
};

struct pakHeader_t {
	int				ident;
	int				dirOfs;
	int				dirLen;
};

/*
 ==============================================================================

    TGA files are used for uncompressed textures

 ==============================================================================
*/

#define TGA_IMAGE_COLORMAP				1
#define TGA_IMAGE_TRUECOLOR				2
#define TGA_IMAGE_MONOCHROME			3
#define TGA_IMAGE_RLE_COLORMAP			9
#define TGA_IMAGE_RLE_TRUECOLOR			10
#define TGA_IMAGE_RLE_MONOCHROME		11

struct tgaHeader_t {
	byte				bIdLength;
	byte				bColormapType;
	byte				bImageType;
	word				wColormapIndex;
	word				wColormapLength;
	byte				bColormapSize;
	word				wXOrigin;
	word				wYOrigin;
	word				wWidth;
	word				wHeight;
	byte				bPixelSize;
	byte				bAttributes;
};

/*
 ==============================================================================

    PCX files are used for uncompressed textures

 ==============================================================================
*/

struct pcxHeader_t {
    char			manufacturer;
    char			version;
    char			encoding;
    char			bitsPerPixel;
    unsigned short	xMin;
	unsigned short	yMin;
	unsigned short	xMax;
	unsigned short	yMax;
    unsigned short	hRes;
	unsigned short	vRes;
    unsigned char	palette[48];
    char			reserved;
    char			colorPlanes;
    unsigned short	bytesPerLine;
    unsigned short	paletteType;
    char			filler[58];
    unsigned char	data;					// Unbound
};

/*
 ==============================================================================

    WAL files are used for uncompressed textures

 ==============================================================================
*/

#define	MIP_LEVELS			4

struct mipTex_t {
	char			name[32];
	unsigned		width;
	unsigned		height;
	unsigned		offsets[MIP_LEVELS];	// Four mipmaps stored
	char			animName[32];			// Next frame in animation chain
	int				flags;
	int				contents;
	int				value;
};

/*
 ==============================================================================

    WAV files are used for sounds

 ==============================================================================
*/

#define WAV_FORMAT_PCM					1

struct wavFormat_t {
	word				wFormat;
	word				wChannels;
	dword				dwSamplesPerSec;
	dword				dwAvgBytesPerSec;
	word				wBlockAlign;
	word				wBitsPerSample;
};

/*
 ==============================================================================

    MD2 files are used for animated (dynamic) render and clip models

 ==============================================================================
*/

#define MD2_IDENT			(('2'<<24)+('P'<<16)+('D'<<8)+'I')
#define MD2_VERSION			8

#define	MD2_MAX_TRIANGLES	4096
#define MD2_MAX_VERTS		2048
#define MD2_MAX_SKINS		32
#define MD2_MAX_FRAMES		512

struct md2St_t {
	short			s;
	short			t;
};

struct md2Triangle_t {
	short			indexXyz[3];
	short			indexSt[3];
};

struct md2Vertex_t {
	byte			v[3];					// Scaled byte to fit in frame mins/maxs
	byte			lightNormalIndex;
};

struct md2Frame_t {
	vec3_t			scale;					// Multiply byte verts by this
	vec3_t			translate;				// Then add this
	char			name[16];				// Frame name from grabbing
	md2Vertex_t		verts[1];				// Variable sized
};

struct md2Header_t {
	int				ident;
	int				version;

	int				skinWidth;
	int				skinHeight;
	int				frameSize;				// Byte size of each frame

	int				numSkins;
	int				numXyz;
	int				numSt;					// Greater than numXyz for seams
	int				numTris;
	int				numGLCmds;				// dwords in strip/fan command list
	int				numFrames;

	int				ofsSkins;				// Each skin is a MAX_QPATH string
	int				ofsSt;					// Byte offset from start for ST verts
	int				ofsTris;				// Offset for triangles
	int				ofsFrames;				// Offset for first frame
	int				ofsGLCmds;	
	int				ofsEnd;					// End of file
};

/*
 ==============================================================================

    BSP files are used for maps

 ==============================================================================
*/

#define BSP_ID				(('P'<<24)+('S'<<16)+('B'<<8)+'I')
#define BSP_VERSION			38

#define	MAX_STYLES			4

#define	LIGHTMAP_WIDTH		128
#define	LIGHTMAP_HEIGHT		128

#define	VIS_PVS				0
#define	VIS_PHS				1

// Upper design bounds
#define	BSP_MAX_ENTITIES	0x000800
#define	BSP_MAX_ENTSTRING	0x040000
#define	BSP_MAX_PLANES		0x010000
#define	BSP_MAX_VERTEXES	0x010000
#define	BSP_MAX_VISIBILITY	0x100000
#define	BSP_MAX_NODES		0x010000
#define	BSP_MAX_TEXINFO		0x002000
#define	BSP_MAX_FACES		0x010000
#define	BSP_MAX_LIGHTING	0x200000
#define	BSP_MAX_LEAFS		0x010000
#define	BSP_MAX_LEAFFACES	0x010000
#define	BSP_MAX_LEAFBRUSHES 0x010000
#define	BSP_MAX_EDGES		0x01F400
#define	BSP_MAX_SURFEDGES	0x03E800
#define	BSP_MAX_MODELS		0x000400
#define	BSP_MAX_BRUSHES		0x002000
#define	BSP_MAX_BRUSHSIDES	0x010000
#define	BSP_MAX_AREAS		0x000100
#define	BSP_MAX_AREAPORTALS	0x000400
#define	BSP_MAX_PORTALS		0x010000

enum headerLump_t {
	LUMP_ENTITIES,
	LUMP_PLANES,
	LUMP_VERTICES,
	LUMP_VISIBILITY,
	LUMP_NODES,
	LUMP_TEXINFO,
	LUMP_FACES,
	LUMP_LIGHTING,
	LUMP_LEAFS,
	LUMP_LEAFFACES,
	LUMP_LEAFBRUSHES,
	LUMP_EDGES,
	LUMP_SURFEDGES,
	LUMP_MODELS,
	LUMP_BRUSHES,
	LUMP_BRUSHSIDES,
	LUMP_POP,
	LUMP_AREAS,
	LUMP_AREAPORTALS,
	NUM_HEADER_LUMPS
};

struct bspLump_t {
	int					offset;
	int					length;
};

struct bspHeader_t {
	int					id;
	int					version;

	bspLump_t			lumps[NUM_HEADER_LUMPS];
};

struct bspModel_t {
	vec3_t				mins;
	vec3_t				maxs;

	vec3_t				origin;					// For sounds or lights

	int					headNode;

	int					firstFace;				// Submodels just draw faces
	int					numFaces;				// without walking the BSP tree
};

struct bspVertex_t {
	vec3_t				point;
};

struct bspPlane_t {
	vec3_t				normal;
	float				dist;
	int					type;
};

struct bspNode_t {
	int					planeNum;
	int					children[2];			// Negative numbers are -(leafs+1), not nodes
	
	short				mins[3];				// For frustum culling
	short				maxs[3];

	unsigned short		firstFace;
	unsigned short		numFaces;				// Counting both sides
};

struct bspTexInfo_t {
	float				vecs[2][4];				// [st][xyz offset]
	int					flags;					// Miptex flags + overrides
	int					value;					// Light emission, etc...
	char				texture[32];			// Texture name (textures/*.wal)
	int					nextTexInfo;			// For animations, -1 = end of chain
};

// Note that edge 0 is never used, because negative edge nums are used 
// for counterclockwise use of the edge in a face
struct bspEdge_t {
	unsigned short		v[2];					// Vertex numbers
};

struct bspFace_t {
	unsigned short		planeNum;
	short				side;

	int					firstEdge;				// We must support > 64k edges
	short				numEdges;	
	short				texInfo;

	// Lighting info
	byte				styles[MAX_STYLES];
	int					lightOfs;				// Start of samples
};

struct bspLeaf_t {
	int					contents;				// OR of all brushes (not needed?)

	short				cluster;
	short				area;

	short				mins[3];				// For frustum culling
	short				maxs[3];

	unsigned short		firstLeafFace;
	unsigned short		numLeafFaces;

	unsigned short		firstLeafBrush;
	unsigned short		numLeafBrushes;
};

struct bspBrushSide_t {
	unsigned short		planeNum;				// Facing out of the leaf
	short				texInfo;
};

struct bspBrush_t {
	int					firstSide;

	int					numSides;
	int					contents;
};

// The visibility lump consists of a header with a count, then byte
// offsets for the PVS and PHS of each cluster, then the raw compressed
// bit vectors
struct bspVis_t {
	int					numClusters;
	int					bitOfs[8][2];
};

// Each area has a list of portals that lead into other areas when 
// portals are closed, other areas may not be visible or hearable even 
// if the vis info says that it should be
struct bspAreaPortal_t {
	int					portalNum;
	int					otherArea;
};

struct bspArea_t {
	int					numAreaPortals;
	int					firstAreaPortal;
};


#else
FILE_WARNING
#endif   // __FILEFORMATS_H__